package sliver.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.alibaba.fastjson.annotation.JSONField;

@Entity
@Table(name = "box_goods")
@Access(AccessType.FIELD)
public class Goods implements Serializable{

	private static final long serialVersionUID = -3044352689574910420L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;
	
	@Column(name = "name")
	private String name;
	
	@Column(name = "min_image_name")
	private String minImageName;
	
	@Column(name = "mid_image_name")
	private String midImageName;
	
	@Column(name = "max_image_name")
	private String maxImageName;
	
	@Column(name = "min_image_path")
	private String minImagePath;
	
	@Column(name = "mid_image_path")
	private String midImagePath;
	
	@Column(name = "max_image_path")
	private String maxImagePath;
	
	@Column(name = "description")
	private String description;
	
	@OneToMany(mappedBy = "goods", cascade = CascadeType.ALL)
	private Set<Size> sizes = new HashSet<Size>();
	
	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "box_rel_goods_category", 
			joinColumns = {@JoinColumn(name = "goods_id")},
			inverseJoinColumns = {@JoinColumn(name = "category_id")})
	private List<Category> categories = new ArrayList<Category>();
	
	@ManyToOne
	@JoinColumn(name = "shopper_id")
	private Shopper shopper;
	
	public Goods(){
		
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@JSONField(serialize = false, deserialize = false)
	public String getMinImageName() {
		return minImageName;
	}

	public void setMinImageName(String minImageName) {
		this.minImageName = minImageName;
	}

	@JSONField(serialize = false, deserialize = false)
	public String getMidImageName() {
		return midImageName;
	}

	public void setMidImageName(String midImageName) {
		this.midImageName = midImageName;
	}

	@JSONField(serialize = false, deserialize = false)
	public String getMaxImageName() {
		return maxImageName;
	}

	public void setMaxImageName(String maxImageName) {
		this.maxImageName = maxImageName;
	}

	public String getMinImagePath() {
		return minImagePath;
	}

	public void setMinImagePath(String minImagePath) {
		this.minImagePath = minImagePath;
	}

	@JSONField(serialize = false, deserialize = false)
	public String getMidImagePath() {
		return midImagePath;
	}

	public void setMidImagePath(String midImagePath) {
		this.midImagePath = midImagePath;
	}

	@JSONField(serialize = false, deserialize = false)
	public String getMaxImagePath() {
		return maxImagePath;
	}

	public void setMaxImagePath(String maxImagePath) {
		this.maxImagePath = maxImagePath;
	}

	@JSONField(serialize = false, deserialize = false)
	public Double getPrice() {
		for(Size size : this.sizes){
			
			if(size.getDefaultSize() == true)
				return size.getPrice();
			
		}
		
		return 0.0D;
	}
	
	@JSONField(serialize = false, deserialize = false)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@JSONField(serialize = false, deserialize = false)
	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	@JSONField(serialize = false, deserialize = false)
	public Shopper getShopper() {
		return shopper;
	}

	public void setShopper(Shopper shopper) {
		this.shopper = shopper;
	}
	
	@JSONField(serialize = false, deserialize = false)
	public Set<Size> getSizes() {
		return sizes;
	}

	public void setSizes(Set<Size> sizes) {
		this.sizes = sizes;
	}
}
